#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QFileInfo>
#include <QDebug>
#include <QFile>
#include <QStandardPaths>
#include <QDir>
#include "common/Utils.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    /********************1.初始化检查配置文件*******************/
    // 获取“我的文档”路径
    QString documentsPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

    // 创建名为“20KWData”的文件夹
    QString folderName = "20KWData";
    QString folderPath = QDir(documentsPath).filePath(folderName);
    QDir dir(folderPath);

    if (!dir.exists()) {
        if (dir.mkdir(".")) {
            qDebug() << "文件夹创建成功：" << folderPath;
        } else {
            qDebug() << "文件夹创建失败";
        }
    } else {
        qDebug() << "文件夹已存在：" << folderPath;
    }

    // 获取并输出文件夹路径
    qDebug() << "文件夹路径：" << folderPath;
    // QString iniFilePath = QCoreApplication::applicationDirPath() + "/Settings.ini";
   QString iniFilePath = folderPath + "/Settings.ini";

    m_MainSettings = new QSettings(iniFilePath,QSettings::IniFormat);
    // 创建QFile对象
    QFile file(iniFilePath);
    // 检查文件是否存在
    if (!file.exists())
    {
        // 文件不存在，尝试创建文件
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            m_MainSettings->setValue("CurrentProject/project","20KW");
            m_MainSettings->setValue("20KW_NetWork/MasterIP","192.168.0.170");
            m_MainSettings->setValue("20KW_NetWork/MasterPort","502");

            for(int i = 1 ; i <= 8;i++)
            {
                m_MainSettings->setValue(QString("20KW_NetWork/Slave%0IP").arg(i),QString("192.168.0.%1").arg(170+i));
                m_MainSettings->setValue(QString("20KW_NetWork/Slave%0Port").arg(i),"502");
            }

            file.close();
        }
    }
    if(m_MainSettings->value("CurrentProject/OptionDescription").toString() != "20KW|3KW")
    {
        m_MainSettings->setValue("CurrentProject/OptionDescription","20KW|3KW");//说明文件
    }

    /********************2.初始化UI界面*******************/
    ui->setupUi(this);
    loadStyle();//


    /********************3.UI子界面添加配置信息*************/
    QString proName = m_MainSettings->value("CurrentProject/project","20KW").toString();
    ui->widget_1->setProNameDevType(proName.trimmed(),0);

    QString Ip = m_MainSettings->value("20KW_NetWork/MasterIP","192.168.0.170").toString();
    short Port = m_MainSettings->value("20KW_NetWork/MasterPort",502).toInt();
    ui->widget_1->setIPPort(Ip.trimmed(),Port);//总机设置IP


    QList<int> ReadStartAddressList; //开始地址的列表
    QList<int> ReadRegisterCountList;//开始地址的个数
    ReadStartAddressList  << 20;
    ReadRegisterCountList << 6;

    ui->widget_1->setReadStartAddressAndRegisterCount(ReadStartAddressList,ReadRegisterCountList,4);//4为输入寄存器

    ReadStartAddressList.clear();
    ReadRegisterCountList.clear();

    ReadStartAddressList << 2;
    ReadRegisterCountList << 2;

    ui->widget_1->setReadStartAddressAndRegisterCount(ReadStartAddressList,ReadRegisterCountList,3);//3为保持寄存器


    for(int i = 1 ; i <= 8; i++)
    {
        Ip = m_MainSettings->value(QString("20KW_NetWork/Slave%1IP").arg(i),QString("192.168.0.%1").arg(170+i)).toString();
        Port = m_MainSettings->value("20KW_NetWork/Slave%1Port",502).toInt();

        SlavePanelForm*  slavePanel = this->findChild<SlavePanelForm*>(QString("widget_%1").arg(i+1));
        if(slavePanel)
        {
            slavePanel->setProNameDevType(proName.trimmed(),i);
            slavePanel->setIPPort(Ip,Port);//分机设置IP

            ReadStartAddressList.clear();
            ReadRegisterCountList.clear();

            ReadStartAddressList << 4 << 20;
            ReadRegisterCountList << 2 << 22;

            slavePanel->setReadStartAddressAndRegisterCount(ReadStartAddressList,ReadRegisterCountList,4);//4为输入寄存器

            ReadStartAddressList.clear();
            ReadRegisterCountList.clear();

            // ReadStartAddressList << 3;  保持寄存器的复位只写，可不读
            // ReadRegisterCountList << 1;

            // slavePanel->setReadStartAddressAndRegisterCount(ReadStartAddressList,ReadRegisterCountList,3);//3为保持寄存器

        }
    }


    /********************4.UI子界面中的独立线程启动循环读取*************/
    ui->widget_1->startLoopReadRegister(true);

    for(int i = 1 ; i <= 8; i++)
    {
        SlavePanelForm*  slavePanel = this->findChild<SlavePanelForm*>(QString("widget_%1").arg(i+1));
        if(slavePanel)
        {
            slavePanel->startLoopReadRegister(true);
        }
    }



    /********************5.UI子界面中总控的故障传递给其他分控子界面*************/
    connect(ui->widget_1,&MasterPanelForm::sig_Dev1fault,ui->widget_2,&SlavePanelForm::slot_Devfaults);
    connect(ui->widget_1,&MasterPanelForm::sig_Dev2fault,ui->widget_3,&SlavePanelForm::slot_Devfaults);
    connect(ui->widget_1,&MasterPanelForm::sig_Dev3fault,ui->widget_4,&SlavePanelForm::slot_Devfaults);
    connect(ui->widget_1,&MasterPanelForm::sig_Dev4fault,ui->widget_5,&SlavePanelForm::slot_Devfaults);

    connect(ui->widget_1,&MasterPanelForm::sig_Dev5fault,ui->widget_6,&SlavePanelForm::slot_Devfaults);
    connect(ui->widget_1,&MasterPanelForm::sig_Dev6fault,ui->widget_7,&SlavePanelForm::slot_Devfaults);
    connect(ui->widget_1,&MasterPanelForm::sig_Dev7fault,ui->widget_8,&SlavePanelForm::slot_Devfaults);
    connect(ui->widget_1,&MasterPanelForm::sig_Dev8fault,ui->widget_9,&SlavePanelForm::slot_Devfaults);


    /********************6.MainWindow界面设置标题***************************/
    setWindowTitle("固态总控分机");


}

MainWindow::~MainWindow()
{
    ui->widget_1->startLoopReadRegister(false);
    for(int i = 1 ; i <= 8; i++)
    {
        SlavePanelForm*  slavePanel = this->findChild<SlavePanelForm*>(QString("widget_%1").arg(i+1));
        if(slavePanel)
        {
            slavePanel->startLoopReadRegister(false);
        }
    }
    delete ui;
}

//样式
void MainWindow::loadStyle(QString currentStyle)
{
    QString styleFileName;
    if(currentStyle == "black")
    {
        styleFileName = ":/res/qss/style.css";
    }
    else if(currentStyle == "gray")
    {
        styleFileName = ":/res/qss/grayStyle.css";
    }
    else
    {
        return;
    }
    QString style;
    if (!QFileInfo::exists(styleFileName))
    {
        return;
    }
    QFile fh(styleFileName);
    if (fh.open(QFile::ReadOnly | QFile::Text))
    {
        style = fh.readAll();
        fh.close();
    }
    else
    {
        qWarning()<<"open file"<<styleFileName<<"error!";
    }
    qApp->setStyleSheet(style);
}

